<?php
require '../model/Db.php';
$Db = Db::getInstance();
$user_data = $Db->islogin();
if(!$user_data){
	http_response_code(404);
	echo '<h1 style="color:#337ab7">千呼万唤始出来,犹抱琵琶半遮面</h1>';
	exit();
}
if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $_POST['files'], $result)){
    $type = $result[2];
    $timestamp = time();
    $filepath = $_POST['type']==1 ? '/www/wwwroot/syxingchuang.com/nsbbs/public/uploads/photo/':'/www/wwwroot/syxingchuang.com/nsbbs/public/uploads/img/';
    $filename = "{$timestamp}_{$user_data['id']}.{$type}";
    $base64str = str_replace($result[1], '', $_POST['files']);
    if(file_put_contents($filepath.$filename, base64_decode($base64str))){
       //修改数据库值
        if($_POST['type'] == 1){
            $ud_flag = $Db->update([
                table => 'bbs_user',
                map => [
                    photo => $filename
                ],
                where => "id = {$user_data['id']}"
            ]);
        }else{
            header('Content-Type:application/json;charset=utf8');
            echo json_encode([file => $filename]);
        }
    	
    }
}elseif($_POST['type'] == 'cancle' && $_POST['file']){

    unlink('/www/wwwroot/syxingchuang.com/nsbbs/public/uploads/img/'.$_POST['file']);
}